﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using TOUR.SZHOME.Attributes;

namespace tour.szhome.manage.area.Controllers
{
    [SelfOnly]
    public class HelpController : Controller
    {
        //
        // GET: /Help/

        /// <summary>
        /// 上传图片响应
        /// </summary>
        /// <param name="FileData"></param>
        /// <returns></returns>
        public ActionResult UploadFiles(HttpPostedFileBase FileData, string f = "")
        {
            string filename = "";
            string location = Server.MapPath("~/uploadfiles/") + f;
            string dateF = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString().PadLeft(2, '0') + "/";
            location += dateF;
            if (null != FileData)
            {
                try
                {
                    filename = Path.GetFileName(FileData.FileName);//获得文件名
                    filename = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(FileData.FileName).ToString();
                    string fullPathname = Path.Combine(location, filename);
                    saveFile(FileData, location, filename);
                }
                catch (Exception ex)
                {
                    filename = ex.ToString();
                }
            }
            return Content(dateF + filename);
        }

        /// <summary>
        /// 存储图片
        /// </summary>
        /// <param name="postedFile"></param>
        /// <param name="filepath"></param>
        /// <param name="saveName"></param>
        /// <returns></returns>
        [NonAction]
        private bool saveFile(HttpPostedFileBase postedFile, string filepath, string saveName)
        {
            bool result = false;
            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            try
            {
                postedFile.SaveAs(Path.Combine(filepath, saveName));
                result = true;
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);
            }
            return result;
        }

    }
}
